﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using System.Reflection;

namespace OPCService.Utils
{
    class Util
    {
        private static readonly Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private static readonly String filename = ConfigHelper.ReadConfig("file");
        private static string tagsExcel = AppDomain.CurrentDomain.BaseDirectory + @ConfigHelper.ReadConfig("excel"); //excel配置tag路径

        //从Excel读取各个sheet的tag信息
        public static DataTable GetTags()
        {
            if (!File.Exists(tagsExcel))
            {
                return null;
            }
            DataTable dtTable = new DataTable();
            List<string> rowList = new List<string>();
            ISheet sheet;
            using (var stream = new FileStream(tagsExcel, FileMode.Open))
            {
                stream.Position = 0;
                XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
                sheet = xssWorkbook.GetSheetAt(0);
                IRow headerRow = sheet.GetRow(0);
                if (headerRow == null)
                {
                    return null;
                }
                int cellCount = headerRow.LastCellNum;
                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
                    {
                        dtTable.Columns.Add(cell.ToString());
                    }
                }
                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    if (row == null) continue;
                    if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                        {
                            if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
                            {
                                rowList.Add(row.GetCell(j).ToString());
                            }
                        }
                    }
                    if (rowList.Count > 0)
                        dtTable.Rows.Add(rowList.ToArray());
                    rowList.Clear();
                }
            }

            return dtTable;
        }

        public static Dictionary<string, DataTable> GetAllTags()
        {
            Dictionary<string, DataTable> result = new Dictionary<string, DataTable>();
            List<string> rowList = new List<string>();
            ISheet sheet;

            using (FileStream stream = new FileStream(tagsExcel, FileMode.Open, FileAccess.Read))
            {
                stream.Position = 0;
                XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
                for (int a = 0; a < xssWorkbook.Count; a++)
                {
                    DataTable dtTable = new DataTable();
                    sheet = xssWorkbook.GetSheetAt(a);
                    IRow headerRow = sheet.GetRow(0);
                    if (headerRow == null)
                    {
                        result.Add(sheet.SheetName, dtTable);
                        return result;
                    }
                    
                    int cellCount = headerRow.LastCellNum;
                    for (int j = 0; j < cellCount; j++)
                    {
                        ICell cell = headerRow.GetCell(j);
                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                        {
                            continue;
                        }
                        dtTable.Columns.Add(cell.ToString());
                    }

                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue;
                        //if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                            {
                                if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
                                {
                                    rowList.Add(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    rowList.Add("");
                                }
                            }
                            else
                            {
                                rowList.Add("");
                            }
                        }
                        if (rowList.Count > 0)
                            dtTable.Rows.Add(rowList.ToArray());
                        rowList.Clear();
                    }
                    result.Add(sheet.SheetName, dtTable);
                }
            }
            return result;
        }

        public static String GetVersion()
        {
            var version = Assembly.GetExecutingAssembly().GetName().Version;
            return version.ToString();
        }

    }
}
